var dataURL = 'data/data2.xml';
var dataDetailsBaseURL = 'data/details.xml';
var detailsBaseURL = '/';
var imagesBaseURL = 'images/';

var strings = [
  'Location',             // infoWindow tab header
  'Info',                 // infoWindow tab header
  'zoom here',            // infoWindow text
  'go to event page',     // infowWindow text
  'actions not shown',    // marker list text
  'Zoom in on the map',   // marker list text
  'Actions',              // marker list text
  'Search by City, Country, Continent, or Zip',  // search bar default text
  'Loading',
  'View Actions at 350.org'
];

var map = null;
var loadstarttime = 0;

var TOPO_MAP;
var DOQ_MAP;
var WET_MAP;
var NHD_MAP;
var NED_MAP; 

function initMap(map){
  map.options.showLoading(map);
  map.map.setCenter(new GLatLng(map.options.defaultCenter.lat, map.options.defaultCenter.lon), map.options.defaultZoom);
  //map.map.setUIToDefault();
  map.map.addControl(new GMapTypeControl());
  map.map.removeMapType(G_SATELLITE_MAP);
  map.map.addMapType(G_PHYSICAL_MAP);
  map.map.addControl(new GSmallMapControl());
  map.map.disableScrollWheelZoom();
  map.map.setMapType(G_HYBRID_MAP); 
  
  /*
  //Start Topo Layer
  tileDRG = new GTileLayer(new GCopyrightCollection(""),1,17);
  tileDRG.myLayers='DRG';
  tileDRG.myFormat='image/jpeg';
  tileDRG.myBaseURL='http://www.terraserver-usa.com/ogcmap6.ashx?';
  tileDRG.getTileUrl=CustomGetTileUrl;
  tileDRG.getOpacity = function() {return 1.0;}
  var topo_layer = [tileDRG];
  TOPO_MAP = new GMapType(topo_layer, G_SATELLITE_MAP.getProjection(), "Topo"); 
  
  //Start DOQQ Layer 
  tileDoq = new GTileLayer(new GCopyrightCollection(""),1,17);
  tileDoq.myLayers='doq';
  tileDoq.myFormat='image/jpeg';
  tileDoq.myBaseURL='http://terraservice.net/ogcmap.ashx?';
  tileDoq.getTileUrl=CustomGetTileUrl;
  var doq_layer=[tileDoq];
  DOQ_MAP = new GMapType(doq_layer, G_SATELLITE_MAP.getProjection(), "DOQQ"); 
  
  //Start NWI Layer 
  tileWET = new GTileLayer(new GCopyrightCollection(""),1,17); 
  tileWET.myLayers='WET'; 
  tileWET.myBaseURL='http://wetlandswms.er.usgs.gov/wmsconnector/ com.esri.wms.Esrimap? ServiceName=USFWS_WMS_CONUS_Wetlands&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG: 4326&LAYERS=CONUS_Wetland_Polygons&STYLES=&FORMAT=image/png'; 
  tileWET.getTileUrl=CustomGetTileUrl;
  tileWET.getOpacity = function() {return 0.5;};
  var WET_layer=[tileWET]; 
  WET_MAP = new GMapType(WET_layer, G_SATELLITE_MAP.getProjection(), "Poss. Wetlands"); 
  
  //Start NHD Layer 
  tileNHD = new GTileLayer(new GCopyrightCollection(""),1,17); 
  tileNHD.myLayers='Nhd'; 
  tileNHD.myFormat='image/gif'; 
  tileNHD.myBaseURL='http://nhdgeo.usgs.gov/wmsconnector/ com.esri.wms.Esrimap/nhdgeowms?VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG: 4326&WIDTH=512&HEIGHT=512&LAYERS=nhdflowline_high,nhdwaterbody_high&STYLES=default,default&TRANSPARENT=TRUE&FORMAT=image/ gif'; 
  tileNHD.getTileUrl=CustomGetTileUrl; 
  var NHD_layer=[tileNHD];
  NHD_MAP = new GMapType(NHD_layer, G_SATELLITE_MAP.getProjection(), "Blue Lines");
  
  //Start Relief Layer 
  var min = 0; 
  var max = 10; 
  var license = "<a href='http://www.maps-for-free.com'>Relief Maps</a>"; 
  var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 0, license); 
  var copyrightCollection = new GCopyrightCollection(""); 
  copyrightCollection.addCopyright(copyright); 
  var relief = [new GTileLayer(copyrightCollection, min, max)]; 
  relief[0].getTileUrl = function(a,b) { return "http://srtm.in-ulm.de/layer/relief_water/z" + b + "/ row" + a.y + "/" + b + "_" + a.x + "-" + a.y + ".jpg"; }; 
  relief[1] = G_HYBRID_MAP.getTileLayers()[1]; 
  layer = new GMapType(relief, G_SATELLITE_MAP.getProjection(), "Relief", {minResolution:min, maxResolution:max});

  //Start Seamless Layer 
  tileNED = new GTileLayer(new GCopyrightCollection(""),1,17); 
  tileNED.myLayers='Ned'; 
  tileNED.myFormat='image/jpeg'; 
  tileNED.myBaseURL='http://columbo.nrlssc.navy.mil/ogcwms/servlet/ WMSServlet/The_National_Map.wms? ServiceName=OGC:WMS&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&SRS=EPSG: 4326&LAYERS=4637:-4652:12460'; 
  tileNED.getTileUrl=CustomGetTileUrl; 
  var NED_layer=[tileNED]; 
  NED_MAP = new GMapType(NED_layer, G_SATELLITE_MAP.getProjection(), "NED");
  
  
  map.map.addMapType(G_NORMAL_MAP);
  map.map.addMapType(G_SATELLITE_MAP);
  map.addMapType(G_PHYSICAL_MAP);
  map.addMapType(TOPO_MAP);
  map.addMapType(NHD_MAP);
  map.addMapType(DOQ_MAP);
  map.addMapType(WET_MAP);
  map.addMapType(NED_MAP);
  map.addMapType(layer); 
  */
}

function readyMap(map){
	$('#console')[0].innerHTML = "Load Time: "+((new Date()).getTime()-loadstarttime)+"<br />\n";
}

function showInfo(map, marker) {
  if (!marker.data.address)
    marker.data.address = marker.data.desc = strings[8]+'...';
  var locTab = new GInfoWindowTab(strings[0], getLocTabHtml(marker.data));
  var infoTab = new GInfoWindowTab(strings[1], getInfoTabHtml(marker.data));
  map.map.openInfoWindowTabs(marker.getLatLng(), [locTab, infoTab]);
}

function getLocTabHtml(data) {
  return '<div id="infoTabLocation"><div class="title">'+data.title+'</div><div class="address">'+data.address+'</div><div class="nav"><a href="#" onclick="return CUSTOMMAP.mapZoom(14,'+data.lat+','+data.lon+');">'+strings[2]+'</a><a href="">'+strings[3]+'</a></div></div>';
}

function getInfoTabHtml(data) {
  return '<div id="infoTabInfo"><div class="desc">'+data.desc+'</div><div class="nav"><a href="">'+strings[3]+'</a></div></div>';
}

function createMap(mapElem) {
	loadstarttime = (new Date()).getTime();
  map = SOCIALHIVE.map.createMap(mapElem, {
    debug: true,
    enableHistory: true,
    historyName: 'map',
    imagesBase: imagesBaseURL,
    defaultZoom: 1,
    defaultCenter: {lat:25.8, lon:13.359},
    dataFeed: dataURL,
    dataType: 'xml', 
    onMarkerClick: showInfo,
    onMapInit: initMap,
		onReady: readyMap,
    maxMarkers: 5000,
    maxListSize: 100
  });
  return map;  
}

